package MCQReaderInterface;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import org.jpedal.Display;
import org.jpedal.PdfDecoder;
import org.jpedal.exception.PdfException;

import MCQReaderSystem.Orienter;

public class MCQReaderDisplay extends JLabel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private PdfDecoder decoder;

	private Image pdfImage;

	private int currentPageNum;

	private int nbPage;

	MCQReaderDisplay(String path) throws Exception {
		setBorder(BorderFactory.createLineBorder(new Color(100, 155, 0)));
		setHorizontalAlignment(JLabel.CENTER);

		decoder = new PdfDecoder();
		decoder.setDisplayView(Display.SINGLE_PAGE, Display.DISPLAY_CENTERED);
		decoder.openPdfFile(path);
		currentPageNum = 1;
		nbPage = decoder.getPageCount();
		decoder.decodePage(currentPageNum);
		pdfImage = decoder.getPageAsImage(currentPageNum);

		/* Debut fonction de rotation */
		BufferedImage bimg = new BufferedImage(pdfImage.getWidth(this),
				pdfImage.getHeight(this), BufferedImage.TYPE_INT_RGB);
		bimg.createGraphics().drawImage(pdfImage, null, null);
		File f = new File("papicurent.gif");
		try {
			if (!ImageIO.write(bimg, "gif", f))
				JOptionPane.showMessageDialog(null, "Ecriture impossible : ");
		} catch (IOException e) {
		}
		Orienter or = new Orienter(f.getAbsolutePath());

		if (or.getPoint1x() != or.getPoint2x()) {

			or.orienter("papicurent.gif");

			Orienter nouveau = new Orienter("kane.gif");
			or = nouveau;
			File fileImg = new File("kane.gif");
			try {
				pdfImage = ImageIO.read(fileImg);
			} catch (IOException e) {
				pdfImage = null;
				System.err.println("Fichier invalide");
			}

		}
		System.out.println("(x1:" + or.getPoint1x() + ", y1:" + or.getPoint1y()
				+ ")");
		/* Fin fonction de rotation */

		setIcon(new ImageIcon(pdfImage));
		MainInterface.thisIsIt.getRightFrame().getNavigator().OpenPDF(nbPage);
		MainInterface.thisIsIt.getLeftFrame().getTree().openPDF(nbPage);
		System.out.println(this.pdfHeight() + " " + this.pdfWidth());
	}

	public Image getCurrentImage() {
		return pdfImage;
	}

	public void previousPage() {
		currentPageNum--;
		try {
			decoder.decodePage(currentPageNum);
			pdfImage = decoder.getPageAsImage(currentPageNum);
			setIcon(new ImageIcon(pdfImage));
		} catch (PdfException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (currentPageNum == 1)
			MainInterface.thisIsIt.getRightFrame().getNavigator()
					.getPreviousBtn().setEnabled(false);

		if (currentPageNum < nbPage + 1)
			MainInterface.thisIsIt.getRightFrame().getNavigator().getNextBtn()
					.setEnabled(true);
		MainInterface.thisIsIt.getRightFrame().getNavigator().getIndexPage()
				.setText("" + currentPageNum + "/" + nbPage);
	}

	public void nextPage() {
		currentPageNum++;
		try {
			decoder.decodePage(currentPageNum);
			pdfImage = decoder.getPageAsImage(currentPageNum);
			// inserer ici fonction de rotation
			setIcon(new ImageIcon(pdfImage));
		} catch (PdfException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		if (currentPageNum > 1)
			MainInterface.thisIsIt.getRightFrame().getNavigator()
					.getPreviousBtn().setEnabled(true);

		if (currentPageNum == nbPage)
			MainInterface.thisIsIt.getRightFrame().getNavigator().getNextBtn()
					.setEnabled(false);
		MainInterface.thisIsIt.getRightFrame().getNavigator().getIndexPage()
				.setText("" + currentPageNum + "/" + nbPage);
	}

	public int pdfXmouse(int x) {
		Dimension d = getSize();
		int widthVariation = (int) ((d.width - decoder.getCurrentPageCoords().width) / 2);
		return (x - widthVariation);
	}

	public int pdfWidth() {
		return (int) pdfImage.getWidth(null);
	}

	public int pdfHeight() {
		return (int) pdfImage.getHeight(null);
	}

	public int getCurrentPageNum() {
		return currentPageNum;
	}

	public int getNbPage() {
		return nbPage;
	}

	public void setCurrentPage(int currentPageNum) {
		this.currentPageNum = currentPageNum;
		try {
			decoder.decodePage(currentPageNum);
			pdfImage = decoder.getPageAsImage(currentPageNum);
			// inserer ici fonction de rotation
			setIcon(new ImageIcon(pdfImage));
		} catch (PdfException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
